Completed
Push — master ( 6412ce...991ce7 )
by
unknown
02:35
created

revision.js ➔ getDocumentRevision   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
nc 8
dl 0
loc 23
rs 6.7272
cc 7
nop 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A revision.js ➔ ... ➔ ??? 0 5 2
1
import fse from 'fs-extra'
2
import path from 'path'
3
import {
4
  cli
0 ignored issues
show
Unused Code introduced by
The variable cli seems to be never used. Consider removing it.
Loading history...
5
  ,FileParser
6
  ,fileUtils
7
  ,dateUnslug
0 ignored issues
show
Unused Code introduced by
The variable dateUnslug seems to be never used. Consider removing it.
Loading history...
8
  ,config
9
  ,fileAttr
10
  ,Manager
11
} from '../../'
12
13
export function getFilesRevision(urls, fileName) {
14
  var res = []
15
  var number = 1
16
  var tplUrl = FileParser.getFileDataFromUrl(fileName)
17
  fileName = fileName.split('/')
18
  fileName = fileName[fileName.length - 1]
19
  var publishDate = new Date()
20
  var json = null
21
22
  if(fileUtils.isFile(tplUrl.publish.json)) {
23
    json = FileParser.getJson(tplUrl.publish.json)
24
    if(typeof json !== 'undefined' && json !== null
25
      && typeof json[config.meta.name] !== 'undefined' && json[config.meta.name] !== null) {
26
      publishDate = new Date(json[config.meta.name].latest.date)
27
    }
28
  }
29
30
  var publishVersion = false
31
  urls.forEach(function (urlObj) {
32
    var fileData = fileAttr.get(urlObj.cleanPath)
33
    if(fileData.s === 'd' && fileAttr.delete(urlObj.cleanPath) == fileAttr.delete(fileName)) {
34
      var currentDate = new Date(urlObj.date)
35
      if(currentDate.getTime() > publishDate.getTime()) {
36
        if(!publishVersion && typeof res[res.length - 1] !== 'undefined' && res[res.length - 1] !== null) {
37
          res[res.length - 1].publishedDate = 'same'
38
        }
39
        publishVersion = true
40
        urlObj.publishedDate = 'after'
41
        
42
      }else if(currentDate.getTime() === publishDate.getTime()) {
43
        urlObj.publishedDate = 'same'
44
        publishVersion = true
45
      }else {
46
        urlObj.publishedDate = 'before'
47
      }
48
      urlObj.version = number
49
      number = number + 1
50
51
      var tplUrlObj = FileParser.getFileDataFromUrl(urlObj.path)
52
      if(fileUtils.isFile(tplUrlObj.publish.json)) {
53
        var jsonObj = FileParser.getJson(tplUrlObj.publish.json)
54
        urlObj[config.meta.name] = jsonObj[config.meta.name]
55
      }
56
      res.push(urlObj)
57
    }
58
  })
59
  return res
60
}
61
62
/**
63
 * Filter and array of file path and return the latest version of those files
64
 * @param  {Object} urls object with path to file, filename etc ...
0 ignored issues
show
Documentation introduced by
The parameter urls does not exist. Did you maybe forget to remove this comment?
Loading history...
65
 * @param  {String} type (draft|waiting|valid)
0 ignored issues
show
Documentation introduced by
The parameter type does not exist. Did you maybe forget to remove this comment?
Loading history...
66
 * @return {Object} urls object filtered
67
 */
68
export function getVersions(docPath) {
69
  var files = Manager.instance.getList()
70
  var fileWithoutExtension = docPath.replace('.' + config.files.templates.extension, '.json')
71
72
  var result = []
73
  Array.prototype.forEach.call(files, (file) => {
74
    if (file.path.indexOf(fileWithoutExtension) > -1) {
75
      result = file.revisions
76
    }
77
  })
78
  return result
79
}
80
81
/**
82
 * Return the revision from document html file path
83
 * if the docPath contains abe revision [status]-[date] will try to return this revision
84
 * else it will return the latest revision
85
 * or null
86
 * 
87
 * @param  {String} html path
0 ignored issues
show
Documentation introduced by
The parameter html does not exist. Did you maybe forget to remove this comment?
Loading history...
88
 * @return {Object} file revision | null
89
 */
90
export function getDocumentRevision(docPath) {
91
  var result = null
92
  var documentPath = docPath
93
  var latest = true
94
  if(fileAttr.test(documentPath)){
95
    latest = false
96
    documentPath = fileAttr.delete(documentPath)
97
  }
98
  var revisions = getVersions(documentPath)
99
  if (latest && revisions.length >= 0) {
100
    result = revisions[0]
101
  }else if (!latest) {
102
    Array.prototype.forEach.call(revisions, (revision) => {
103
      if (revision.html === docPath) {
104
        result = revision
105
      }
106
    })
107
    if (result === null && revisions.length >= 0) {
108
      result = revisions[0]
109
    }
110
  }
111
  return result
112
}